package pe.edu.upc.dew.pharma.util;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.SqlReturnResultSet;
import org.springframework.jdbc.object.StoredProcedure;

public class StoredProcedurePharma extends StoredProcedure {
	
	private static final Log logger = LogFactory.getLog(StoredProcedurePharma.class);
	
	private Map<String, Object> hmInParameters;
	
	public StoredProcedurePharma(JdbcTemplate  template, String storeProcedureName) {
		super(template, storeProcedureName);
		hmInParameters = new HashMap<String, Object>();
	}
	
	public void addInParameter(String paramterName, int parameterType, Object value) {
		hmInParameters.put(paramterName, value);
		declareParameter(new SqlParameter(paramterName, parameterType));
	}
	
	public void addOutParameter(String paramterName, int parameterType) {
		declareParameter(new SqlParameter(paramterName, parameterType));
	}
	
	public Map<String, Object> executeProcedure(String name, RowMapper handler){
		declareParameter(new SqlReturnResultSet(name,handler));
		return executeProcedure();
	}
	
	@SuppressWarnings("unchecked")
	public Map<String, Object> executeProcedure(){
		compile();
		if(logger.isInfoEnabled()){
			logger.info("call " + getCallString());
			
		}
		return execute(hmInParameters);
	}
	
}
